package mes.operator.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import mes.operator.dto.ComponentDTO;
import mes.operator.dto.EmployeeDTO;
import mes.operator.dto.HistoryDTO;
import mes.operator.dto.MachineDTO;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class OperatorDAOImpl extends SqlMapClientDaoSupport implements OperatorDAO{
	

	@Override
	public void machineRun() throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public HashMap machineState(String machineName) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 로그인
	 */
	@Override
	public EmployeeDTO login(EmployeeDTO dto) throws SQLException {
		return (EmployeeDTO)this.getSqlMapClient().queryForObject("em.login",dto);
	}
	
	@Override
	public ArrayList<ComponentDTO> getComponentInfo(String component_name, int cnt) throws SQLException{
		HashMap map = new HashMap();
		map.put("component_name", component_name);
		map.put("cnt", cnt);
		return (ArrayList<ComponentDTO>) this.getSqlMapClient().queryForList("operator.getComponentInfo", map);
	}
	
	public void insertComponent(ComponentDTO cdto) throws SQLException{
		this.getSqlMapClient().insert("operator.insertComponent", cdto);
	}

	@Override
	public ArrayList<ComponentDTO> getComponentCount(String component_name) throws SQLException {
		return (ArrayList<ComponentDTO>) this.getSqlMapClient().queryForList("operator.getComponentCount", component_name);
	}

	@Override
	public void updateComponentInfo(ComponentDTO cdto) throws SQLException {
		this.getSqlMapClient().update("operator.updateComponentInfo", cdto);
	}

	@Override
	public void insertMachineA(HashMap map) throws SQLException {
		this.getSqlMapClient().startTransaction();
		try{
			this.getSqlMapClient().insert("machine.insertMachineA", map);
			this.getSqlMapClient().commitTransaction();
		}finally{
			this.getSqlMapClient().endTransaction();
		}
	}

	@Override
	public void updateMachineEndtime(HashMap map) throws SQLException {
		this.getSqlMapClient().startTransaction();
		try{
			this.getSqlMapClient().update("machine.updateMachineEndtime", map);
			this.getSqlMapClient().commitTransaction();
		}finally{
			this.getSqlMapClient().endTransaction();
		}
	}

	@Override
	public void insertMachineB(HashMap map) throws SQLException {
		this.getSqlMapClient().startTransaction();
		try{
			this.getSqlMapClient().insert("machine.insertMachineB", map);
			this.getSqlMapClient().commitTransaction();
		}finally{
			this.getSqlMapClient().endTransaction();
		}
	}

	@Override
	public void insertMachineC(HashMap map) throws SQLException {
		this.getSqlMapClient().startTransaction();
		try{
			this.getSqlMapClient().insert("machine.insertMachineC", map);
			this.getSqlMapClient().commitTransaction();
		}finally{
			this.getSqlMapClient().endTransaction();
		}
	}
	@Override
	public void insertComponentNum(String component_name) throws SQLException {
		HashMap map = new HashMap();
		map.put("component_name", component_name);
		this.getSqlMapClient().insert("operator.insertComponentNum",  map);
		map.clear();
	}

	@Override
	public void insertHistory(HashMap map) throws SQLException {
		this.getSqlMapClient().insert("operator.insertHistory", map);
	}

	@Override
	public ArrayList<HistoryDTO> getHistoryInfo() throws SQLException {
		return (ArrayList<HistoryDTO>) this.getSqlMapClient().queryForList("operator.getHistoryInfo");
	}

	@Override
	public ArrayList<MachineDTO> selectAllMachineByName(String machineName) throws SQLException{
		return (ArrayList<MachineDTO>)this.getSqlMapClient().queryForList("machine.selectAllMachine", machineName);
	}

	@Override
	public ArrayList<MachineDTO> getIsFaultMachineByName(String string) throws SQLException {
		return (ArrayList<MachineDTO>) this.getSqlMapClient().queryForList("machine.getIsFaultMachineByName", string);
	}


}
